﻿using Microsoft.Win32;
using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using static System.Runtime.InteropServices.JavaScript.JSType;

namespace ImageTools
{
    /// <summary>
    /// JpegGenToolUi.xaml 的交互逻辑
    /// </summary>
    public partial class JpegGenToolUi : Window
    {
        public JpegGenToolUi()
        {
            InitializeComponent();
        }

        public void SelectFilesClicked(object sender, RoutedEventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog
            {
                Title = "选择文件",
                Multiselect = true,
                Filter = "Image Files (*.bmp, *.jpg, *.jpeg)|*.bmp;*.jpg;*.jpeg"

            };

            if (openFileDialog.ShowDialog() == true)
            {
                string[] selectedFilePath = openFileDialog.FileNames;

                listbox_files.Items.Clear();

                foreach (string filePath in selectedFilePath)
                {
                    listbox_files.Items.Add(filePath);
                }
            }
        }

        public void button_SaveFileClicked(object sender, RoutedEventArgs e)
        {
            var folderDialog = new OpenFolderDialog();
            if (folderDialog.ShowDialog() == true)
            {
                if (folderDialog.ToString() != string.Empty)
                {
                    textBox_outPath.Text = folderDialog.FolderName;
                }
            }
        }
        public void button_GenClicked(object sender, RoutedEventArgs e)
        {
            List<string> fileNames = new List<string>();

            foreach(string filePath in listbox_files.Items)
            {
                fileNames.Add(filePath);
            }

            int mode = 0;
            if (radioButton_Yuv444.IsChecked == true)
            {
                mode = 0;
            }
            else if (radioButton_Yuv422.IsChecked == true)
            {
                mode = 1;
            }
            else if (radioButton_Yuv420.IsChecked == true)
            {
                mode = 2;
            }

            if (fileNames.Count == 0)
            {
                return;
            }

            if (textBox_outPath.Text == string.Empty)
            {
                MessageBox.Show("输出目录为空！！");
                return;
            }
            
            int ditherMode = 0;

            if (checkBox_isDither_rgb565.IsChecked == true)
            {
                ditherMode = 1;
            }
            else
            {
                ditherMode = 0;
            }

            JpegGenTool jpegGenTool = new JpegGenTool(textBox_outPath.Text, mode, ditherMode);

            string[] paths = fileNames.ToArray();
            string error = "";

            List<Task> taskList = new List<Task>();

            Task task;
            int ret;

            foreach (string path in paths) 
            {
                task = new Task(() =>
                {
                    ret = jpegGenTool.JpegGenOneFile(path);
                    if (ret != 0)
                    {
                        error += jpegGenTool.Error;
                    }
                });

                task.Start();
                
                taskList.Add(task);
            }

            Task.WaitAll(taskList.ToArray());
            
            if (error != "")
            {
                MessageBox.Show(error);
                return;
            }

            MessageBox.Show("转换完成。");
            
        }
    }
}
